package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.product.service.CategoryService;
import com.spzx.product.vo.CategoryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Tag(name = "商品分类管理")
@RestController
@RequestMapping("/category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    @RequiresPermissions("product:category:query")
    @Operation(summary = "获取分类下拉树列表")
    @GetMapping(value = "/treeSelect/{id}")
    public AjaxResult treeSelect(@PathVariable Long id) {
        List<CategoryVo> list = categoryService.treeSelect(id);
        return success(list);
    }

    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("product:category:export")
    @Operation(summary = "商品分类信息导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
        categoryService.exportData(response);
    }

    @Log(title = "分类管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("product:category:import")
    @Operation(summary = "商品分类信息导入")
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) {
        try {
            categoryService.importData(file);
            return AjaxResult.success("文件导入成功");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.success("文件失败成功");
        }
    }

    /**
     * 获取商品一级分类
     *
     * @return
     */
    @InnerAuth
    @GetMapping(value = "/getCategoryLevel1")
    public R<List<CategoryVo>> getCategoryLevel1() {
        return R.ok(categoryService.treeSelect(0l));
    }

    /**
     * 获取商品分类整体树形结构
     *
     * @return
     */
    @InnerAuth
    @GetMapping("/allTree")
    public R<List<CategoryVo>> allTree() {
        return R.ok(categoryService.allTree());
    }
}
